package com.Jiang.controller;

import com.Jiang.pojo.Admin;
import com.Jiang.pojo.User;
import com.Jiang.service.AdminService;
import com.Jiang.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class AdminController {


    @Autowired
    AdminService adminService;
    //1.登录
    @RequestMapping("/adminlogin")
    public String Login(HttpSession httpSession, Admin admin){
        System.out.println("登录");
        String adminName=admin.getAdminName();
        String password=admin.getPassword();
        Map m=new HashMap();
        m.put("adminName",adminName);
        List<Admin> admins=adminService.Login(m);
        if(admins.size()!=0&&admins.get(0).getPassword().equals(password)){
            System.out.println("登录成功!");
            httpSession.setAttribute("Login1",adminName);
            return "redirect:/book/AllBook";
    }

        return "redirect:/loginUI";    //转发到主页
    }

    @RequestMapping("/toAdminRegister")
    public String toAdminRegister(){
        return "/adminRegister";
    }
//    2.管理员修改密码
    @RequestMapping("/toUpdataAdminPassword")
    public String toUpdataPassword(){
        return "UpdataAdminPassword";
    }
    @RequestMapping("/UpdataAdminPassword")
    public String UpdataPassword(HttpSession httpSession,String password){
        String adminName= (String) httpSession.getAttribute("Login1");
        Map m=new HashMap();
        m.put("adminName",adminName);
        m.put("password",password);
        adminService.updateAdminInfo(m);
        return "redirect:/book/AllBook";
    }
    //3.验证
    @ResponseBody
    @RequestMapping("/checkAdOldPassword")
    public String checkOldPassword(HttpSession session, String oldPassword, HttpServletResponse response){
        String userName= (String) session.getAttribute("Login1");
        Map m=new HashMap();
        m.put("userName",userName);
//        String password=userService.Login(m).get(0).getPassword();
        String password=adminService.Login(m).get(0).getPassword();
        if(password.equals(oldPassword)){
            return "√";
        }
        return "×";

    }
    @RequestMapping("/loginadmin")
    public String loginadmin(){
        return "redirect:/";
    }
}
